using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ST.Domain.Entities;
using System.Linq;

namespace ST.EntityFrameworkCore
{
    public static class SeedData
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using var context = new STDbContext(serviceProvider.GetRequiredService<DbContextOptions<STDbContext>>());
            if (context.Users.Any())
            {
                return;   // 已经初始化过数据，直接返回
            }
            var departmentId = Guid.NewGuid();
            //增加一个部门
            context.Departments.Add(
                new Department
                {
                    Id = departmentId,
                    Name = "集团总部",
                    ParentId = Guid.Empty
                }
            );

            //增加四个基本功能菜单
            context.Menus.AddRange(
                new Menu
                {
                    Id = Guid.NewGuid(),
                    Name = "组织机构管理",
                    Code = "Department",
                    SerialNumber = 0,
                    ParentId = Guid.Empty,
                    Icon = "fa fa-link"
                },
                new Menu
                {
                    Id = Guid.NewGuid(),
                    Name = "角色管理",
                    Code = "Role",
                    SerialNumber = 1,
                    ParentId = Guid.Empty,
                    Icon = "fa fa-link"
                },
                new Menu
                {
                    Id = Guid.NewGuid(),
                    Name = "用户管理",
                    Code = "User",
                    SerialNumber = 2,
                    ParentId = Guid.Empty,
                    Icon = "fa fa-link"
                },
                new Menu
                {
                    Id = Guid.NewGuid(),
                    Name = "功能管理",
                    Code = "Department",
                    SerialNumber = 3,
                    ParentId = Guid.Empty,
                    Icon = "fa fa-link"
                }
            );
            var roleId=Guid.NewGuid();
            context.Roles.Add(
                new Role
                {
                    Id = roleId,
                    Name = "超级管理员",
                    CreateTime = DateTime.Now,
                     RoleMenus = context.Menus.Local.Select(i=>new RoleMenu
                     {
                         RoleId = roleId,
                         MenuId = i.Id
                     }).ToList()
                }
            );
            var userId=Guid.NewGuid();
            //增加一个超级管理员用户
            context.Users.Add(
                new User
                {
                    Id = userId,
                    UserName = "admin",
                    Password = "123456", //暂不进行加密
                    Name = "超级管理员",
                    DepartmentId = departmentId,
                    UserRoles = new List<UserRole>()
                    {
                        new UserRole()
                        {
                            RoleId = roleId,
                            UserId = userId
                        }
                    }
                }
            );
            context.SaveChanges();
        }
    }
}